package com.example.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisServiceImp implements RedisService{

	@Autowired
	private StringRedisTemplate redisTemplate;

	@Override
	public void set(final String key, final String value) {
		redisTemplate.opsForValue().set(key, value);
	}

	@Override
	public Object get(final String key) {
		return redisTemplate.opsForValue().get(key);
	}

	@Override
	public Boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	@Override
	public Boolean expire(final String key, final int seconds) {
		return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	}

	@Override
	public Long ttl(final String key) {
		return redisTemplate.getExpire(key);
	}

	@Override
	public Long incr(final String key) {
		return redisTemplate.opsForValue().increment(key);
	}

	@Override
	public void sadd(final String key, final String... values) {
		redisTemplate.opsForSet().add(key, values);
	}

	@Override
	public Set<String> smembers(final String key) {
		return redisTemplate.opsForSet().members(key);
	}

	@Override
	public Long scard(final String key) {
		return redisTemplate.opsForSet().size(key);
	}

	@Override
	public List<String> srandmembers(final String key, final int count) {
		return redisTemplate.opsForSet().randomMembers(key, count);
	}

	@Override
	public Long srem(final String key, final String... value) {
		return redisTemplate.opsForSet().remove(key, value);
	}

	@Override
	public void hset(final String key, final String field, final String value) {
		redisTemplate.opsForHash().put(key, field, value);
	}

	@Override
	public Object hget(final String key, final String field) {
		return redisTemplate.opsForHash().get(key, field);
	}

	@Override
	public Long hdel(final String key, final String... field) {
		return redisTemplate.opsForHash().delete(key, field);
	}

	@Override
	public Object getSet(String key, String value) {
		return redisTemplate.opsForValue().getAndSet(key, value);
	}
}
